Delete Method Example

This example uses the Delete method to remove a specified record from a Recordset. The DeleteRecord procedure is required for this procedure to run

Sub DeleteX()

    Dim dbsNorthwind As Database
    Dim rstEmployees As Recordset
    Dim lngID As Long

    Set dbsNorthwind = OpenDatabase("Northwind.mdb")
    Set rstEmployees = _
        dbsNorthwind.OpenRecordset("Employees")

    ' Add temporary record to be deleted.
    With rstEmployees
        .Index = "PrimaryKey"
        .AddNew
        !FirstName = "Janelle"
        !LastName = "Tebbs"
        .Update
        .Bookmark = .LastModified
        lngID = !EmployeeID
    End With

    ' Delete the employee record with the specified ID 
    ' number.
    DeleteRecord rstEmployees, lngID

    rstEmployees.Close
    dbsNorthwind.Close

End Sub

Sub DeleteRecord(rstTemp As Recordset, _
    lngSeek As Long)

    With rstTemp
        .Seek "=", lngSeek
        If .NoMatch Then
            MsgBox "No employee #" & lngSeek & " in file!"
        Else
            .Delete
            MsgBox "Record for employee #" & lngSeek & _
                " deleted!"
        End If
    End With

End Sub